//
//  ZYCTeachingTeachLearningPaperCell.m
//  ZDSC
//
//  Created by ZDTX on 2021/9/2.
//

#import "ZYCTeachingTeachLearningPaperCell.h"
#import "ZYCTeachingLearnChoiceView.h"
#import "ZYCTeachLearningPaperModel.h"
#import "NSString+Size.h"

#define BTHeightSpace 9

@interface ZYCTeachingTeachLearningPaperCell ()<ZYCTeachingLearnChoiceViewDelegate>
@property (nonatomic, strong) UILabel *questionLabel;
@property (nonatomic, strong) UIView *line;

@property (nonatomic, strong) UIButton *questionButton;

@property (nonatomic, strong) NSMutableArray * choiceViewArray;

@end
@implementation ZYCTeachingTeachLearningPaperCell


#pragma mark - ZYCTeachingLearnChoiceViewDelegate
////subjectType 题目类型（1：单选题；2：多选题；3：简答题）
-(void) choiceView:(ZYCTeachingLearnChoiceView *) choiceView didChoiceOptionId:(ZYCAnswerModel *)answerModel{
    if ([self.model.subjectType integerValue] == 1) {// 单选
        for (ZYCTeachingLearnChoiceView *v  in self.choiceViewArray) {
            v.model.checkFlag = NO;
            v.selected = NO;
        }
        choiceView.selected = YES;
        answerModel.checkFlag = 1;
        self.model.isPaper = YES;
    }else{//多选题
        choiceView.selected = !choiceView.selected;
        answerModel.checkFlag = choiceView.selected;
        self.model.isPaper = YES;
    }

    if (self.selectModelBlock) {
        self.selectModelBlock(self.model);
    }
  
}

- (NSMutableAttributedString *)returnFuwenBen:(NSString *)string{
    NSMutableAttributedString *attriString = [[NSMutableAttributedString alloc] initWithString:string];
    NSRange rang0 = NSMakeRange(0, self.model.sortNum.length);
    [attriString addAttribute:NSFontAttributeName value:kCustomFont(@"PingFangSC-Medium", 16.0f) range:rang0];
    NSRange rang1 = NSMakeRange(self.model.sortNum.length + 1, self.model.subjectTypeStr.length + 1);
    [attriString addAttribute:NSForegroundColorAttributeName value:HEX_RGB(@"#39A85B") range:rang1];
    return attriString;;
    
}



- (void)setModel:(ZYCSubjectModel *)model{
    _model = model;
    _questionLabel.attributedText = [self returnFuwenBen:[NSString stringWithFormat:@"%@、 %@ %@",model.sortNum,model.subjectTypeStr,model.subjectTitle]];
    
    for (ZYCTeachingLearnChoiceView *choiceView in self.choiceViewArray) {
        [choiceView removeFromSuperview];
    }
    
    CGFloat margin = 0.0f;
    for (NSInteger i = 0; i < model.subjectItemVoList.count; i++) {
        ZYCAnswerModel *m = model.subjectItemVoList[i];
        ZYCTeachingLearnChoiceView *choiceView = [[ZYCTeachingLearnChoiceView alloc] init];
        choiceView.model = m;
        CGSize titleSize =  [m.content sizeWithFont:kCustomFont(@"PingFangSC-Light", 16.0f) constrainedToWidth:self.contentView.frame.size.width - 49];
        m.titleHeight = titleSize.height;
        if (i == 0) {
            margin = 15.0f;
        }else{
            margin += m.titleHeight;
        }
        choiceView.subjectType = [model.subjectType integerValue];
        choiceView.selected  = m.checkFlag;
        choiceView.delegate = self;
        [self.contentView addSubview:choiceView];
        [self.choiceViewArray addObject:choiceView];
        if (i == model.subjectItemVoList.count - 1) {// 最后一个
            [choiceView mas_makeConstraints:^(MASConstraintMaker *make) {
                make.left.equalTo(self.contentView).offset(14.0f);
                make.right.equalTo(self.contentView).offset(-14.0f);
                make.top.equalTo(self.line.mas_bottom).offset(margin);
                make.height.mas_equalTo(m.titleHeight);
                make.bottom.equalTo(self.contentView).offset(-15.0f);
            }];

        }else{
            [choiceView mas_makeConstraints:^(MASConstraintMaker *make) {
                make.top.equalTo(self.line.mas_bottom).offset(margin);
                make.left.equalTo(self.contentView).offset(14.0f);
                make.right.equalTo(self.contentView).offset(-14.0f);
                make.height.mas_equalTo(m.titleHeight);
 
            }];
        }
        // 第一次遍历之后需要将第一个的高度加上 否则会布局异常
        margin += m.titleHeight;
    }
}


- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.choiceViewArray = [NSMutableArray arrayWithCapacity:0];
        self.userInteractionEnabled = YES;
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        [self.contentView addSubview:self.questionLabel];
        [self.contentView addSubview:self.line];
        [self.questionLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.top.equalTo(self.contentView).offset(14.0f);
            make.right.equalTo(self.contentView).offset(-14.0f);
        }];
        
        [self.line mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.contentView).offset(14.0f);
            make.right.equalTo(self.contentView).offset(-14.0f);
            make.top.equalTo(self.questionLabel.mas_bottom).offset(14.0f);
            make.height.mas_equalTo(0.5f);
        }];
        
    }
    return self;
}


- (UILabel *) questionLabel{
    if (!_questionLabel) {
        _questionLabel = [[UILabel alloc] init];
        _questionLabel.textAlignment = NSTextAlignmentLeft;
        _questionLabel.font = kCustomFont(@"PingFangSC-Light", 16.0f);
        _questionLabel.numberOfLines = 0;
    }
    return _questionLabel;
}

- (UIView *)line {
    if (!_line) {
        _line = [[UIView alloc] init];
        _line.backgroundColor = HEX_RGB(@"#EBEBEB");
    }
    return _line;
}




@end
